# -*- encoding:utf-8 -*-
# __author__ = 'chenyongbing'
# __poject__ = 'ops_platform'
# __datetime__ = '2022/2/21'
# __name__ = 'serializer.py'
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from apps.system import models
from apps.system.handles import signer

# Create your code here.

# 加密字段方法
class SerializerEncryptField(serializers.CharField):
    def to_internal_value(self):
        pass



class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.User
        fields = ['id', 'username', 'name', 'mobile', 'email']

    # def validate(self, data):
    #     return data


class RoleSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Role
        fields = ['id', 'code', 'name']


class MenuSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Menu
        fields = ['id', 'path', 'code', 'name', 'parent']




class MenuTreeSerializer(serializers.ModelSerializer):
    children = serializers.SerializerMethodField(source='get_children')
    meta = serializers.SerializerMethodField(source='get_title')
    class Meta:
        model = models.Menu
        fields = ['id', 'path', 'meta', 'children', 'code', 'parent']
        
    def get_name(self, obj):
        return obj.code

    def get_meta(self, obj):
        return {'title': obj.name, 'icon': obj.icon}

    def get_children(self, obj):
        children = obj.children.all()
        serializer = MenuTreeSerializer(children, many=True)
        return serializer.data


class MenuTreeForSelectSerializer(serializers.ModelSerializer):
    children = serializers.SerializerMethodField(source='get_children')
    value = serializers.SerializerMethodField(source='get_value')
    label = serializers.SerializerMethodField(source='get_label')

    class Meta:
        model = models.Menu
        fields = ['value', 'label', 'children']

    def get_value(self, obj):
        return obj.id

    def get_label(self, obj):
        return obj.name

    def get_children(self, obj):
        children = obj.children.all()
        serializer = MenuTreeForSelectSerializer(children, many=True)
        if serializer.data == []:
            return None
        return serializer.data


class ConfigSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Config
        fields ="__all__"


    def create(self, validated_data):
        encrypt = validated_data.get('encrypt')
        value = validated_data.get('value')
        if encrypt and value:
            newvalue = signer.sign(value)
            validated_data['value'] = newvalue
        obj = self.Meta.model.objects.create(**validated_data)
        if obj:
            return validated_data
        else:
            raise ValidationError('创建失败')


    def update(self, instance, validated_data):
        encrypt = validated_data.get('encrypt')
        value = validated_data.get('value')
        if encrypt and value:
            newvalue = signer.sign(value)
            validated_data['value'] = newvalue
        instance.value = validated_data.get('value')
        instance.encrypt = encrypt
        instance.name = validated_data.get('name')
        instance.save()
        return instance

class ConfigItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Config
        fields = ['id', 'group', 'name', 'item', 'value', 'encrypt']

class ConfigGroupSerializer(serializers.ModelSerializer):
    items = serializers.SerializerMethodField()
    class Meta:
        model = models.Config
        fields = ['group', 'items']

    def get_items(self, obj):
        config = self.Meta.model.objects.filter(group=obj.get('group'))
        serializer = ConfigItemSerializer(config, many=True)
        if serializer.data == []:
            return None
        return serializer.data